const Models = require('./../../models')

class Order{
    static async addOrder(ctx){
        const req = ctx.request.body;
        const { userId, carId, time, money} = req;
        if(req.userId && req.carId && req.time&& req.money){
            try{
                await Models.orders.create({
                    userId,
                    carId,
                    time,
                    money,
                    createdAt: new Date(),
                    updatedAt: new Date(),
                })
                await Models.cars.update({isUse: false},{
                    where: {
                        id: req.carId
                    }
                })
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 200,
                    msg: '创建成功'
                }
            }catch (e) {
                ctx.response.status = 200;
                ctx.response.body = {
                    code: 516,
                    msg: '创建失败'
                }
            }
        }else{
            ctx.response.status = 200;
            ctx.response.body = {
                code: 416,
                msg: '数据不完整'
            }
        }
    }

    static async getUserOrders(ctx){
        const req = ctx.request.body;
        const data = await Models.orders.findAll({
            where: {
                userId: req.userId
            }
        })
        if(data){
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '数据获取成功',
                data
            }
        }else{
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '无订单'
            }
        }
    }

    static async getOrders(ctx){
        try{
            const data =await Models.orders.findAll();
            ctx.response.status = 200;
            ctx.response.body = {
                code: 200,
                msg: '订单获取成功',
                data
            }
        }catch (e) {
            ctx.response.status = 200;
            ctx.response.body = {
                code: 501,
                msg: '订单获取失败'
            }
        }
    }

    static async passOrder(ctx){
        const req = ctx.request.body;
        await Models.orders.update({isPass: true},{
            where: {
                userId: req.userId,
                carId: req.carId
            }
        })
        await Models.leasingmanagements.create({
            userId: req.userId,
            carId: req.carId,
            time: req.time,
            money: req.money,
            createdAt: new Date(),
            updatedAt: new Date()
        })
        const info =await Models.informats.findOne({
            where: {
                id: 1
            }
        })
        await Models.informats.update({amount: info.amount + req.money}, {
            where: {
                id: 1
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '请求成功'
        }
    }

    static async deleteOrder(ctx){
        const req = ctx.request.body;
        if (req.isPass === true){
            await Models.orders.destroy({
                where: {
                    userId: req.userId,
                    carId: req.carId
                }
            })
        }else{
            await Models.orders.destroy({
                where: {
                    userId: req.userId,
                    carId: req.carId
                }
            })
            await Models.cars.update({isUse: true},{
                where: {
                    carId: req.carId
                }
            })
        }
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '数据删除成功'
        }
    }

    static async passAllOrder(ctx){
        const data = await Models.orders.findAll({
            where: {
                isPass: false
            }
        })
        await Models.orders.update({isPass: true},{
            where: {
                isPass: false
            }
        })
        await Promise.all(data.map(async (item)=> {
            await Models.leasingmanagements.create({
                userId: item.userId,
                carId: item.carId,
                time: item.time,
                money: item.money,
                createdAt: new Date(),
                updatedAt: new Date()
            })
        }))
        const info =await Models.informats.findOne({
            where: {
                id: 1
            }
        })
        const moneys = data.reduce((pre,cur) => {
            pre = pre + cur.money
            return pre;
        },info.amount)
        await Models.informats.update({amount: moneys},{
            where: {
                id: 1
            }
        })
        ctx.response.status = 200;
        ctx.response.body = {
            code: 200,
            msg: '全部审核通过'
        }
    }

}

module.exports = Order